﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using YWTSSys.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using YWTSSys.Model;

namespace YWTSSys.DAL
{
    public  class CustomerGroupDAL
    {
        public static bool AddArea(string 分组标志)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("CustomerGroup_AddArea", 分组标志);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool AddGroup(CustomerGroupInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("CustomerGroup_AddGroup", obj.分组标志, obj.UserId, obj.上级分组, obj.IsDefault);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool DeleteArea(string 分组标志)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("CustomerGroup_DeleteArea", 分组标志);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool DeleteGroup(string 分组标志)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("CustomerGroup_DeleteGroup", 分组标志);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetAllArea()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("CustomerGroup_GetAllArea");
            return DB.ExecuteReader(dbCommand);
	    }
        public static DataTable GetAllGroupByArea(string AreaName)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("CustomerGroup_GetAllGroupByArea", AreaName);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        public static bool UpdateUser(string 分组标志, int UserId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("CustomerGroup_UpdateUser", 分组标志, UserId);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public static IDataReader GetByGroupName(string groupName)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("CustomerGroup_GetByGroupName", groupName);
            return DB.ExecuteReader(dbCommand);
        }
    }
}
